package com.alpha.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.alpha.bom.ExamResponse;
import com.alpha.bom.Professor;
import com.alpha.bom.Student;
import com.alpha.dao.ExamResponseDao;

/**
 * Servlet implementation class StudentResponse
 */
@WebServlet("/StudentResponseServlet")
public class StudentResponseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(StudentResponseServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentResponseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" get method started.");
				
		int examResponseId = 0;
		ExamResponse examResponse = null;
		
		HttpSession session = request.getSession();
		
		ExamResponseDao erDao = new ExamResponseDao();
		
		if(request.getParameter("exam_response_id")!=null ){
			examResponseId = Integer.parseInt(request.getParameter("exam_response_id"));
			try {
				examResponse = erDao.retrieveExamResponse(examResponseId);
			} catch (Exception e) {
				if(session.getAttribute("user") instanceof Professor){
					logger.error(e.getMessage());
					response.sendRedirect("ExamsAdminServlet?error=Exam+Response+not+found");
					return;					
				} else {
					logger.error(e.getMessage());
					response.sendRedirect("MyExamServlet?error=Exam+Response+not+found");
					return;
				}
			} 
		} else {
			if(session.getAttribute("user") instanceof Professor){
				response.sendRedirect("ExamsAdminServlet?error=Exam+Response+not+found");
				return;					
			} else {
				response.sendRedirect("MyExamServlet?error=Exam+Response+not+found");
				return;
			}
		}
				
		if(examResponse!=null){
			request.setAttribute("exam_response", examResponse);
			RequestDispatcher dispatcher = request.getRequestDispatcher("StudentResponse.jsp");
            dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info(this.getClass().getSimpleName()+" post method started.");
		
		HttpSession session = request.getSession();
		ExamResponse examResponse = null;
		Student student = null;
		
		//student is either the Student user or a null public user
		if(session.getAttribute("user")!=null && (session.getAttribute("user") instanceof Student)){
			student = (Student) session.getAttribute("user");
		}
		
		if(session.getAttribute("exam_response") != null) {
			examResponse = (ExamResponse) session.getAttribute("exam_response");
			if(student!=null){
				ExamResponseDao erDao = new ExamResponseDao();
				try {
					erDao.updateDateTaken(examResponse.getExam().getExamId(), student.getUserId(), new Date());
				} catch (NamingException e) {
					logger.error(e.getMessage());
					return;
				} catch (SQLException e) {
					logger.error(e.getMessage());
					return;
				}
			}
			examResponse.setComplete(true);	
			session.removeAttribute("exam_response");
			request.setAttribute("exam_response", examResponse);
			RequestDispatcher dispatcher = request.getRequestDispatcher("StudentResponse.jsp");
			dispatcher.forward(request, response);
		}
	}
}
